{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Install and Setup Kubeflow on EKS\n",
    "\n",
    "Based on these instructions:  https://www.kubeflow.org/docs/aws/deploy/install-kubeflow/\n",
    "\n",
    "# _This Will Take a Few Minutes, Please Be Patient._"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    ". ~/.bash_profile\n",
    "\n",
    "export AWS_CLUSTER_STATUS=$(aws eks describe-cluster --name ${AWS_CLUSTER_NAME} --region ${AWS_REGION} --query \"cluster.status\" --output text)\n",
    "\n",
    "if [ -n \"$AWS_CLUSTER_STATUS\" ]; then\n",
    "    if [ $AWS_CLUSTER_STATUS == \"ACTIVE\" ]; then\n",
    "        echo \"Cluster status:  $AWS_CLUSTER_STATUS.\"\n",
    "    else\n",
    "        echo \"Cluster status:  $AWS_CLUSTER_STATUS.  Please wait for status:  ACTIVE\"\n",
    "        exit\n",
    "    fi\n",
    "fi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DO NOT CONTINUE UNTIL THE CLUSTER STATUS IS `ACTIVE` ^^ ABOVE ^^."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    ". ~/.bash_profile\n",
    "\n",
    "echo $S3_BUCKET\n",
    "echo $AWS_CLUSTER_NAME\n",
    "echo $INSTANCE_ROLE_NAME\n",
    "echo $INSTANCE_PROFILE_ARN\n",
    "\n",
    "cp kfext/kfctl /usr/local/bin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    ". ~/.bash_profile\n",
    "\n",
    "export KF_NAME=${AWS_CLUSTER_NAME}\n",
    "echo \"export KF_NAME=${KF_NAME}\" | tee -a ~/.bash_profile\n",
    "\n",
    "export KF_DIR=$PWD/${KF_NAME}\n",
    "echo \"export KF_DIR=${KF_DIR}\" | tee -a ~/.bash_profile\n",
    "\n",
    "mkdir -p ${KF_DIR}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    ". ~/.bash_profile\n",
    "\n",
    "cp kfext/v1.0.2.tar.gz ${KF_DIR}\n",
    "cp kfext/kfctl_aws.v1.0.2.yaml ${KF_DIR}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    ". ~/.bash_profile\n",
    "\n",
    "export CONFIG_FILE=${KF_DIR}/kfctl_aws.v1.0.2.yaml\n",
    "echo \"export CONFIG_FILE=${CONFIG_FILE}\" | tee -a ~/.bash_profile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    ". ~/.bash_profile\n",
    "\n",
    "sed -i.bak -e \"/region: us-west-2/ a \\      enablePodIamPolicy: true\" ${CONFIG_FILE}\n",
    "sed -i.bak -e \"s@us-west-2@$AWS_REGION@\" ${CONFIG_FILE}\n",
    "sed -i.bak -e \"s@roles:@#roles:@\" ${CONFIG_FILE}\n",
    "sed -i.bak -e \"s@- eksctl-kubeflow-aws-nodegroup-ng-a2-NodeInstanceRole-xxxxxxx@#- eksctl-kubeflow-aws-nodegroup-ng-a2-NodeInstanceRole-xxxxxxx@\" ${CONFIG_FILE}\n",
    "sed -i.bak -e \"s@eksctl-kubeflow-aws-nodegroup-ng-a2-NodeInstanceRole-xxxxxxx@$INSTANCE_ROLE_NAME@\" ${CONFIG_FILE}\n",
    "sed -i.bak -e 's/kubeflow-aws/'\"$AWS_CLUSTER_NAME\"'/' ${CONFIG_FILE}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# This Next Cell Takes a Few Minutes.  Please Be Patient. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    ". ~/.bash_profile\n",
    "\n",
    "cd ${KF_DIR}\n",
    "\n",
    "rm -rf kustomize\n",
    "rm -rf .cache\n",
    "\n",
    "kfctl build -V -f ${CONFIG_FILE}\n",
    "\n",
    "cd ${KF_DIR}\n",
    "\n",
    "kfctl apply -V -f ${CONFIG_FILE}\n",
    "\n",
    "kubectl delete all -l app=spartakus --namespace=kubeflow\n",
    "\n",
    "kubectl config set-context --current --namespace=kubeflow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Wait for resource to become available\n",
    "Monitor changes by running kubectl get all namespaces command.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl -n kubeflow get all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl -n istio-system get all"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Release Resources"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%html\n",
    "\n",
    "<p><b>Shutting down your kernel for this notebook to release resources.</b></p>\n",
    "<button class=\"sm-command-button\" data-commandlinker-command=\"kernelmenu:shutdown\" style=\"display:none;\">Shutdown Kernel</button>\n",
    "        \n",
    "<script>\n",
    "try {\n",
    "    els = document.getElementsByClassName(\"sm-command-button\");\n",
    "    els[0].click();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}    \n",
    "</script>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%javascript\n",
    "\n",
    "try {\n",
    "    Jupyter.notebook.save_checkpoint();\n",
    "    Jupyter.notebook.session.delete();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "instance_type": "ml.t3.medium",
  "kernelspec": {
   "display_name": "Python 3 (Data Science)",
   "language": "python",
   "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/datascience-1.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
